<template>
  <div class="appStatisticPage">
    <h2>热门应用统计</h2>
    <v-chart :option="AppCountOption" style="height: 300px" />
    <a-input-search
            :style="{ width: '320px' }"
            placeholder="输入 appId"
            button-text="搜索"
            search-button
            @search="(value) => loadAppAnswerResultCount(value)"
    />

    <h2>应用结果统计</h2>
    <v-chart :option="AppAnswerResultOption" style="height: 300px" />
  </div>
</template>

<script setup lang="ts">
  import {reactive, ref, watchEffect,onMounted,computed} from 'vue'
  import {listUserByPageUsingPost} from "@/api/userController";
  import message from "@arco-design/web-vue/es/message";
  import {listAppVoByPageUsingPost} from "@/api/appController";
  import AppCard from "@/components/AppCard.vue"
  import {countAppAnswerResultUsingGet, countAppAnswerUsingGet} from "@/api/appStatisticController";
  import VChart from "vue-echarts";
  import "echarts";

  const AppCountOption = computed(() =>{
    return{
      xAxis: {
        type: 'category',
        data: appCountList.value.map((item) => item.appId),
        name: "应用Id"
      },
      yAxis: {
        type: 'value',
        name:"用户答案数"
      },
      series: [
        {
          data: appCountList.value.map((item) => item.appCount),
          type: 'bar'
        }
      ]
    };
  })

  const AppAnswerResultOption = computed(() =>{
    return {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: '应用答案结果分布',
          type: 'pie',
          radius: '50%',
          data: appAnswerResultCountList.value.map((item) => {
            return{
              value:item.resultNameCount,
              name:item.resultName
            }
          }),
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
  })

  //应用统计数据
  const appCountList = ref<API.AppAnswerCountDTO[]>([])
  //答案结果统计数据
  const appAnswerResultCountList = ref<API.AppAnswerResultCountDTO[]>([])

  //调用接口获取应用数据
  const loadAppCount = async () =>{
    const res = await countAppAnswerUsingGet();
    if(res.data.code === 0){
      appCountList.value = res.data.data || [];
    }else{
      message.error("获取失败" + res.data.message)
    }
  }

  const loadAppAnswerResultCount = async (appId:string) =>{
    if(!appId){
      return;
    }
    const res = await countAppAnswerResultUsingGet({
      appId:appId as any,
    });
    if(res.data.code === 0){
      appAnswerResultCountList.value = res.data.data || [];
    }else{
      message.error("获取失败" + res.data.message)
    }
  }


  // /**
  //  * 执行搜索
  //  */
  // const doSearch = () => {
  //   searchParam.value = {
  //     ...initSearchParam,
  //     ...formSearchParams.value,
  //   };
  // };

  /**
   * 监听 searchParams 变量，改变时触发数据的重新加载
   */
  watchEffect(() =>{
    loadAppAnswerResultCount("")
  })

  /**
   * 监听 searchParams 变量，改变时触发数据的重新加载
   */
  watchEffect(() =>{
    loadAppCount()
  })

</script>

<style scoped>
  .searchBar {
    text-align: center;
    margin-bottom: 28px;
  }

  .list-demo-action-layout .image-area {
    width: 183px;
    height: 119px;
    overflow: hidden;
    border-radius: 2px;
  }

  .list-demo-action-layout .list-demo-item {
    padding: 20px 0;
    border-bottom: 1px solid var(--color-fill-3);
  }

  .list-demo-action-layout .image-area img {
    width: 100%;
  }

  .list-demo-action-layout .arco-list-item-action .arco-icon {
    margin: 0 4px;
  }
</style>
